//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Query Application Block
//===============================================================================

using System.Configuration;
using System.Data;
using EntLibContrib.Query.Commands;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ContainerModel;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace EntLibContrib.Query.Configuration
{
    using Database.Properties;
    using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;

    /// <summary>
	/// Configuration data defining DataCustomCommandData.
	/// </summary>
    [ResourceDescription(typeof(DesignResources), "DataCommandDataDescription")]
    [ResourceDisplayName(typeof(DesignResources), "DataCommandDataDisplayName")]
    [AddSateliteProviderCommand("connectionStrings", typeof(DatabaseSettings), "DefaultDatabase", "DatabaseInstanceName")]
    [System.ComponentModel.Browsable(true)]
    public class DataCommandData : CommandData
	{
        private const string databaseInstanceNameProperty = "databaseInstanceName";
        private const string commandProperty = "command";
        private const string commandTypeProperty = "commandType";

		/// <summary>
		/// Initializes a new instance of the <see cref="DataCommandData"/> class.
		/// </summary>
		public DataCommandData ()
            : this(null)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="DataCommandData"/> class.
		/// </summary>
		/// <param name="name">The provider name.</param>
		public DataCommandData(string name)
			: base(name, typeof(DataCommand))
		{
		}

        /// <summary>
        /// Name of the data provider.
        /// </summary>
        [ConfigurationProperty(nameProperty, DefaultValue = "Data Command")]
        public override string Name
        {
            get { return base.Name; }
            set { base.Name = value; }
        }

        /// <summary>
        /// Name of the database instance to be used by the command. Instance must be defined in Data configuration.
        /// </summary>
        [ConfigurationProperty(databaseInstanceNameProperty, IsRequired = true)]
        [ResourceDescription(typeof(DesignResources), "DataCommandDataDatabaseInstanceNameDescription")]
        [ResourceDisplayName(typeof(DesignResources), "DataCommandDataDatabaseInstanceNameDisplayName")]
        [Reference(typeof(ConnectionStringSettingsCollection), typeof(ConnectionStringSettings))]
        public string DatabaseInstanceName
        {
            get { return (string)base[databaseInstanceNameProperty]; }
            set { base[databaseInstanceNameProperty] = value; }
        }

        /// <summary>
        /// The command name.
        /// </summary>
        [ConfigurationProperty(commandProperty, IsRequired = true)]
        [ResourceDescription(typeof(DesignResources), "DataCommandDataCommandDescription")]
        [ResourceDisplayName(typeof(DesignResources), "DataCommandDataCommandDisplayName")]
        public string Command
        {
            get { return (string)base[commandProperty]; }
            set { base[commandProperty] = value; }
        }

        /// <summary>
        /// The command type.
        /// </summary>
        [ConfigurationProperty(commandTypeProperty, IsRequired = true, DefaultValue = CommandType.Text)]
        [ResourceDescription(typeof(DesignResources), "DataCommandDataCommandTypeDescription")]
        [ResourceDisplayName(typeof(DesignResources), "DataCommandDataCommandTypeDisplayName")]
        public CommandType CommandType
        {
            get { return (CommandType)base[commandTypeProperty]; }
            set { base[commandTypeProperty] = value; }
        }

        /// <summary>
        /// A <see cref="TypeRegistration"/> container configuration model for <see cref="XmlFileCommandData"/>.
        /// </summary>
        /// <returns>A <see cref="TypeRegistration"/> for registering a <see cref="XmlFileCommandData"/> in the container.</returns>
        public override TypeRegistration GetRegistration()
        {
            return
                new TypeRegistration<ICommand>(
                    () => new DataCommand(
                        Container.Resolved<Microsoft.Practices.EnterpriseLibrary.Data.Database>(DatabaseInstanceName),
                        Command,
                        CommandType,
                        Container.ResolvedIfNotNull<IParameterSet>(ParameterSetName)))
                {
                    Lifetime = TypeRegistrationLifetime.Transient,
                    Name = Name,
                    IsPublicName = true
                };
        }

	}
}